package com.sl.au.repository;

import java.util.List;
import java.util.Set;

//import org.activiti.engine.impl.Page;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import com.sl.au.entity.Module;

@Transactional(readOnly = true)
public interface ModuleRepository extends JpaRepository<Module, String> {
	/**
	 * 模块的快速查询
	 * 
	 * @param key
	 * @param pageable
	 * @return
	 */
	@Query(" select m from Module m where m.menuId=2 and (m.moduleName like %?1% or m.name like %?1%) order by m.menuIndex")
	Page<Module> getModuleName(String key, Pageable pageable);
	/**
	 * 得到模块数据条数
	 * @param key
	 * @return
	 */
	@Query(" select count(*) as num from Module m where  m.menuId=2 and (m.moduleName like %?1% or m.name like %?1%)")
	Long getModuleCount(String key);
	/**
	 * 
	 * @param menuId
	 * @return
	 */
	@Query(" select m from Module m where m.menuId=?1 order by m.menuIndex")
	List<Module> findBymenuId(String menuId);

	List<Module> findByparentId(String pId);

	List<Module> findByparentIdOrderByMenuIndex(String pId);

	@Modifying
	@Query("update Module m set m.menuId= ?1 , m.parentId = ?2 where m.id=?3")
	int setModuleNode(String menuId, String parentId, String id);

	Module findById(String parentId);

	@Query(" select m from Module m where m.menuId=?1 order by m.menuIndex")
	Page<Module> getData(String i, Pageable pageable);
	/**
	 * 得到模块数据条数
	 * @return
	 */
	@Query(" select count(*) as num from Module m where m.menuId=?1")
	Long getCount(String j);
	/**
	 * 通过角色得到其所有的功能模块
	 * 
	 * @param roleId
	 * @return
	 */
	@Query("select m from Module m where  m.id in ( select mr.module.id from M2R  mr where mr.role.id=?1)")
	Set<Module> findModulesByRoleId(String roleId);

	/**
	 * 通过角色得到其所有的功能模块一级菜单
	 * 
	 * @param roleId
	 * @return
	 */
	@Query("select m from Module m where  m.id in ( select mr.module.id from M2R  mr where mr.role.id=?1) and m.menuLevel>0 order by m.menuLevel")
	Set<Module> findModulesMenu1(String roleId);
	
	@Query("select m from Module m where  m.id in ( select mr.module.id from M2R  mr where mr.role.id=?1) and m.menuLevel >0 order by m.menuLevel asc, m.menuIndex asc")
	List<Module> findModulesMenu2(String roleId);

	@Query("select m from Module m where  m.id in ( select mr.module.id from M2R  mr where mr.role.id=?1) and m.menuLevel >0 and m.groupId=?2 order by m.menuLevel asc, m.menuIndex asc")
	List<Module> findModulesMenu3(String roleId, String group);
	
	@Query("select m from Module m where  m.id in ( select mr.module.id from M2R  mr where mr.role.id=?1) and m.parentId in (select mm.id from Module mm where mm.name = ?2 ) order by m.menuLevel asc, m.menuIndex asc")
	List<Module> findModulesMenuAndParent(String roleId, String parentId);

	Module findByName(String moduleName); 
	
}